﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace In66.Net.Core.System.Json
{
    public static class NewtonTextJson
    {
        public static JsonSerializerSettings GetDefaultOptions()
        {
            var options = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new SnakeCaseNamingStrategy()
                }
            };

            options.ContractResolver = new NullToEmptyStringResolver();
            options.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            options.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            return options;
        }

        public static NullToEmptyStringResolver GetNullToEmptyStringResolver()
        {
            return new NullToEmptyStringResolver();
        }

        #region Converter

        public class NullToEmptyStringResolver : DefaultContractResolver
        {
            protected override IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
            {
                return type.GetProperties()
                    .Select(p =>
                    {
                        var jp = base.CreateProperty(p, memberSerialization);
                        jp.ValueProvider = new NullToEmptyStringValueProvider(p);
                        return jp;
                    }).ToList();
            }
        }
        public class NullToEmptyStringValueProvider : Newtonsoft.Json.Serialization.IValueProvider
        {
            private PropertyInfo _MemberInfo;
            public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
            {
                _MemberInfo = memberInfo;
            }
            public object GetValue(object target)
            {
                object result = _MemberInfo.GetValue(target);
                if (_MemberInfo.PropertyType == typeof(string) && result == null)
                {
                    result = "";
                }
                else if (_MemberInfo.PropertyType == typeof(String[]) && result == null)
                {
                    result = new string[] { };
                }
                else if (_MemberInfo.PropertyType == typeof(Nullable<Int32>) && result == null)
                {
                    result = 0;
                }
                else if (_MemberInfo.PropertyType == typeof(Nullable<long>) && result == null)
                {
                    result = 0L;
                }
                else if (_MemberInfo.PropertyType == typeof(Nullable<Decimal>) && result == null)
                {
                    result = 0.00M;
                }
                else if (_MemberInfo.PropertyType == typeof(Nullable<DateTime>) && result == null)
                {
                    result = DateTime.Today;
                }

                //if (result == null) 
                //	result = "";
                return result;
            }
            public void SetValue(object target, object value)
            {
                _MemberInfo.SetValue(target, value);
            }
        }

        #endregion
    }
}
